package jdk8的特性.Stream.查找和匹配;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author idea
 * @data 2018/9/9
 */
public class Test {


    public static void main(String[] args) {

        List<User> users = MockDB.mockUserList(20);
        //匹配
        if (users.stream().anyMatch(User::isGirl)) {
            System.out.println("存在女生");
        }
        if (users.stream().allMatch(user -> user.getAge() > 0)) {
            System.out.println("用户的年龄全部符合");
        }
        if (users.stream().noneMatch(user -> user.getId() < -1)) {
            System.out.println("用户的所有id均符合");
        }
        //查找
        AtomicBoolean hasUser= new AtomicBoolean(false);
        Optional<List<User>> userOptional=Optional.ofNullable(users);
        //如果包含就赋值操作
        userOptional.ifPresent(u->{
            hasUser.set(true);
            return;
        });
        System.out.println(hasUser);

        //查找第一个元素
        List<Integer> numberList= Arrays.asList(1,2,3,4,5,6,7,8,9);
        //Optional主要用于判断是否存在
        Optional<Integer> firstNumberOptional=numberList.stream()
                .map(x->x*x)
                .filter(x->x%3==0)
                .findAny();
        System.out.println(firstNumberOptional.isPresent());
    }

}
